﻿using B.S.DataBase.MyProject.Domain.HealthMonitoring;
using B.S.DataBase.MyProject.Domain.ZJWWorkOrderTwo;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure;
using MediatR;
using MedicalPCTerminal.Write.API.Appcation.Commands.ZJWWorkOrderTwoCommands;

namespace MedicalPCTerminal.Write.API.Appcation.CommandHandlers.ZJWWorkOrderTwoCommandHandlers
{
    public class UpdateMedicalCardCommandHandler : IRequestHandler<UpdateMedicalCardCommand, APIResult<int>>
    {
        /// <summary>
        /// SqlSugar
        /// </summary>
        private readonly SqlSugarContext sqlSugar;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="sqlSugar">SqlSugar</param>
        public UpdateMedicalCardCommandHandler(SqlSugarContext sqlSugar)
        {
            this.sqlSugar = sqlSugar;
        }
        public async Task<APIResult<int>> Handle(UpdateMedicalCardCommand request, CancellationToken cancellationToken)
        {
            try
            {
                APIResult<int> result = new APIResult<int>();
                var list = sqlSugar.db.Queryable<MedicalCard>().Where(x => x.CardId == request.CardId).First();
                list.PatientName = request.PatientName;
                list.Gender = request.Gender;
                list.Age = request.Age;
                list.IdNumber = request.IdNumber;
                list.Phone = request.Phone;
                list.Address = request.Address;
                list.CardStatus = request.CardStatus;
                var res = sqlSugar.db.Updateable(list).ExecuteCommand();
                if (res > 0)
                {
                    result.Code = APIEnum.修改成功;
                    result.Msg = APIEnum.修改成功.ToString();
                    result.data = res;
                }
                else
                {
                    result.Code = APIEnum.修改失败;
                    result.Msg = APIEnum.修改失败.ToString();
                    result.data = res;
                }

                return await Task.FromResult(result);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
